/*
 * JGO2RDF creates a RDF representation of GAF2 files, which are automatically sourced from GO ftp site.
 * Copyright (C) 2012 Matthew M. Hindle & Artem Lysenko
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package uk.co.jgo2rdf.goa.goa2rdf;

import uk.co.jgo2rdf.goa.deligates.*;
import uk.co.jgo2rdf.goa.goa2rdf.deligates.GOARDFizer;
import uk.co.jgo2rdf.goa.goa2rdf.deligates.GenericGOAParser;
import uk.co.jgo2rdf.goa.goa2rdf.deligates.TAIRGeneGOAParser;
import uk.co.jgo2rdf.rdf.lsrn.LSRNVocab;
import uk.co.jgo2rdf.rdf.refseq.RefSeqFactory;
import uk.co.jgo2rdf.rdf.refseq.URIs;
import uk.co.jgo2rdf.uniprotidmapping.UniprotIdMappingIndex;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Pattern;

/**
 * Created by IntelliJ IDEA.
 * User: mhindle
 * Date: 23/02/12
 * Time: 18:37
 * To change this template use File | Settings | File Templates.
 */
public class DelegateFactory {

    //GAF2 file --> delegate for dealing with GOA entries
    private static Map<String, GOARDFizer> delegates = new HashMap<String, GOARDFizer>();

    public static Map<String, GOARDFizer> getDelegates() {
        return delegates;
    }

    /**
     * initalize which delegates deal with which type of file, and which sub-delegates deal which which database ids
     * this means that rdfizing a given file is very customizable
     */
    static {
        Pattern postColon = Pattern.compile(":?(\\d+)");

        try {
            //UniprotIdMappingIndex index = new UniprotIdMappingIndex("/home/mhindle/UniProt/idmapping", true);

            TAIRGeneGOAParser tairGene = new TAIRGeneGOAParser(true);
            tairGene.addEntityRDFizer("TAIR", new TAIRRDFizer());
            tairGene.addEntityRDFizer("AGI_LocusCode", new TAIRRDFizer());
            delegates.put("gene_association.tair.gz", tairGene);
        } catch (IOException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
        GenericGOAParser generic = new GenericGOAParser();
        generic.addEntityRDFizer("UniProtKB", new UniProtRDFizer());
        generic.addEntityRDFizer("GR_protein", new UniProtRDFizer());

        generic.addEntityRDFizer("GR_gene",
                new GenericGenomicDBRDFizer(URIs.GRAMENE, LSRNVocab.GRAMENE_Record, LSRNVocab.GRAMENE_Identifier));
        generic.addEntityRDFizer("ZFIN",
                new GenericGenomicDBRDFizer(URIs.ZFIN, LSRNVocab.ZFIN_Record, LSRNVocab.ZFIN_Identifier));
        generic.addEntityRDFizer("AspGD",
                new GenericGenomicDBRDFizer(URIs.AspGD, LSRNVocab.AspGD_Record, LSRNVocab.AspGD_Identifier));
        generic.addEntityRDFizer("AspGD_Locus",
                new GenericGenomicDBRDFizer(URIs.AspGD_Locus, LSRNVocab.AspGD_Locus_Record, LSRNVocab.AspGD_Locus_Identifier));
        generic.addEntityRDFizer("CGD",
                new GenericGenomicDBRDFizer(URIs.CGD, LSRNVocab.CGD_Record, LSRNVocab.CGD_Identifier));
        generic.addEntityRDFizer("CGD_Locus",
                new GenericGenomicDBRDFizer(URIs.CGD_Locus, LSRNVocab.CGD_Locus_Record, LSRNVocab.CGD_Locus_Identifier));

        generic.addEntityRDFizer("dictyBase",
                new GenericGenomicDBRDFizer(URIs.DictyBase, LSRNVocab.DictyBase_Record, LSRNVocab.DictyBase_Identifier));
        generic.addEntityRDFizer("FB",
                new GenericGenomicDBRDFizer(URIs.FLYBASE, LSRNVocab.FLYBASE_Record, LSRNVocab.FLYBASE_Identifier));
        generic.addEntityRDFizer("GeneDB_Lmajor",
                new GenericGenomicDBRDFizer(URIs.Lmajor, LSRNVocab.Lmajor_Record, LSRNVocab.Lmajor_Identifier));
        generic.addEntityRDFizer("GeneDB_Pfalciparum",
                new GenericGenomicDBRDFizer(URIs.Pfalciparum, LSRNVocab.Pfalciparum_Record, LSRNVocab.Pfalciparum_Identifier));
        generic.addEntityRDFizer("GeneDB_Tbrucei",
                new GenericGenomicDBRDFizer(URIs.Tbrucei, LSRNVocab.Tbrucei_Record, LSRNVocab.Tbrucei_Identifier));
        generic.addEntityRDFizer("MGI",
                new GenericGenomicDBRDFizer(URIs.MGI, LSRNVocab.MGI_Record, LSRNVocab.MGI_Identifier));
        generic.addEntityRDFizer("ASAP",
                new GenericGenomicDBRDFizer(URIs.ASAP, LSRNVocab.ASAP_Record, LSRNVocab.ASAP_Identifier));

        generic.addEntityRDFizer("SGD",
                new GenericGenomicDBRDFizer(URIs.SGD, LSRNVocab.SGD_Record, LSRNVocab.SGD_Identifier));
        generic.addEntityRDFizer("SGN",
                new GenericGenomicDBRDFizer(URIs.SGN, LSRNVocab.SGN_Record, LSRNVocab.SGN_Identifier));
        generic.addEntityRDFizer("RGD",
                new GenericGenomicDBRDFizer(URIs.RGD, LSRNVocab.RGD_Record, LSRNVocab.RGD_Identifier));
        generic.addEntityRDFizer("WB",
                new GenericGenomicDBRDFizer(URIs.WORMBASE_GENE, LSRNVocab.WB_GENE_Record, LSRNVocab.WB_GENE_Identifier));

        generic.addEntityRDFizer("TIGR_CMR",
                new GenericGenomicDBRDFizer(URIs.TIGR_CMR, LSRNVocab.TIGR_CMR_Record, LSRNVocab.TIGR_CMR_Identifier));

        generic.addEntityRDFizer("JCVI_CMR",
                new GenericGenomicDBRDFizer(URIs.JVCI_CMR, LSRNVocab.JVCI_CMR_Record, LSRNVocab.JVCI_CMR_Identifier));

        GenericGenomicDBRDFizer rdfizer = new GenericGenomicDBRDFizer(URIs.VMD, LSRNVocab.VMD_Record, LSRNVocab.VMD_Identifier, postColon);
        generic.addEntityRDFizer("PAMGO_VMD", rdfizer);

        delegates.put("gene_association.gramene_oryza.gz", generic);
        delegates.put("gene_association.goa_uniprot.gz", generic);
        delegates.put("gene_association.goa_uniprot_noiea.gz", generic);
        delegates.put("gene_association.goa_pig.gz", generic);
        delegates.put("gene_association.goa_human.gz", generic);
        delegates.put("gene_association.goa_dog.gz", generic);
        delegates.put("gene_association.goa_cow.gz", generic);
        delegates.put("gene_association.goa_chicken.gz", generic);

        delegates.put("gene_association.PAMGO_Atumefaciens.gz", generic);

        delegates.put("gene_association.zfin.gz", generic);
        delegates.put("gene_association.aspgd.gz", generic);
        delegates.put("gene_association.cgd.gz", generic);
        delegates.put("gene_association.dictyBase.gz", generic);
        delegates.put("gene_association.fb.gz", generic);
        delegates.put("gene_association.GeneDB_Lmajor.gz", generic);
        delegates.put("gene_association.GeneDB_Pfalciparum.gz", generic);
        delegates.put("gene_association.GeneDB_Tbrucei.gz", generic);
        delegates.put("gene_association.GeneDB_tsetse.gz", generic);
        delegates.put("gene_association.mgi.gz", generic);
        delegates.put("gene_association.PAMGO_Ddadantii.gz", generic);
        delegates.put("gene_association.PAMGO_Mgrisea.gz", generic);
        delegates.put("gene_association.PAMGO_Oomycetes.gz", generic);

        delegates.put("gene_association.sgd.gz", generic);
        delegates.put("gene_association.rgd.gz", generic);
        delegates.put("gene_association.sgn.gz", generic);
        delegates.put("gene_association.wb.gz", generic);

        delegates.put("gene_association.jcvi.gz", generic);

        //add support for refseq everywhere
        RefSeqRDFizer refSeqProcessor = new RefSeqRDFizer();
        for (String refseqType : RefSeqFactory.supportedRefSeqTypes) {
            generic.addEntityRDFizer("NCBI_" + refseqType, refSeqProcessor);
        }
        generic.addEntityRDFizer("NCBI", refSeqProcessor);
        generic.addEntityRDFizer("REFSEQ_PROT", refSeqProcessor);
        generic.addEntityRDFizer("NCBI_GP", new GeneBankRDFizer());
    }

}
